<?php
   /*
   Plugin Name: DMCI Project Listings
   Description: a plugin to add project listings for DMCI Theme
   Version: 1.0
   Author: Perry Mark Guinjicna
   License: GPL2
   */
   
   include_once('includes/project-logo-slider.php');
   include_once('includes/project-metaboxes.php');
   include_once('includes/project-textareas.php');
   include_once('includes/helpers.php');
   
   add_action('init', 'create_projects');
   
   function create_projects(){
		
		$proj_spec = array(
			'labels' => array(
				'name' => 'Projects',
				'singluar_name' => 'Project',
				'add_new' => 'Add New',
				'add_new_item' => 'Add New Project',
				'edit' => 'Edit',
				'edit_item' => 'Edit Project',
				'new_item' => 'New Project',
				'view' => 'View',
				'view_item' =>'View Project',
				'search_items' => 'Search Project',
				'not_found' => 'No projects found',
				'not_found_in_trash' => 'No projects in trash',
				'parent' => 'Parent Projects'
			),
			'public' => true,
			'menu_position' => 15,
			'supports' => array( 'title', 'taxonomy', 'excerpt', 'thumbnail', ),
			'taxonomies' => array( '' ),
			'rewrite' => array('slug' => 'project', 'with_front' => FALSE),
			'query_var' => true,
			'has_archive' => false
		);
		
		
		register_post_type( 'dmci_projects' , $proj_spec );
		
		
		$proj_update_spec = array(
			'labels' => array(
				'name' => 'Construction Updates',
				'singluar_name' => 'Construction Update',
				'add_new' => 'Add New',
				'add_new_item' => 'Add New Construction Update',
				'edit' => 'Edit',
				'edit_item' => 'Edit Construction Update',
				'new_item' => 'New Construction Update',
				'view' => 'View',
				'view_item' =>'View Construction Update',
				'search_items' => 'Search Construction Update',
				'not_found' => 'No Construction updates found',
				'not_found_in_trash' => 'No Construction updates in trash',
				'parent' => 'Parent Projects'
			),
			'public' => true,
			'menu_position' => 15,
			'supports' => array( 'title', 'taxonomy', 'editor'),
			'taxonomies' => array( '' ),
			'rewrite' => array('slug' => 'construction-update', 'with_front' => FALSE),
			'query_var' => true,
			'has_archive' => false
		);
		
		
		register_post_type( 'dmci_cons_update' , $proj_update_spec );
	    
	}
	
	add_action( 'add_meta_boxes', 'change_excerpt_meta_box', 0 );
	function change_excerpt_meta_box() {
			global $wp_meta_boxes;
			unset( $wp_meta_boxes['post']['side']['core']['postexcerpt'] );
			add_meta_box('postexcerpt',
			__('Short Project Description'),
			'post_excerpt_meta_box',
			'dmci_projects',
			'side',
			'low');
	}
	
	add_filter( 'template_include', 'include_template_function', 1 );
	
	function include_template_function( $template_path ) {
		
		if ( get_post_type() == 'dmci_projects' ) {
			if ( is_single() ) {
				// checks if the file exists in the theme first,
				// otherwise serve the file from the plugin
				if ( $theme_file = locate_template( array ( 'single-dmci_projects.php' ) ) ) {
					$template_path = $theme_file;
				} else {
					$template_path = plugin_dir_path( __FILE__ ) . 'template/single-dmci_projects.php';
				}
			}
			else if( is_tax() ) {
				
				if ( $theme_file = locate_template( array ( 'project-category.php' ) ) ) {
					$template_path = $theme_file;
				} else {
					$template_path = plugin_dir_path( __FILE__ ) . 'template/project-category.php';
				}
				
			}
			
			
		}
		
		if ( get_post_type() == 'dmci_cons_update' ) {
			if ( is_single() ) {
				// checks if the file exists in the theme first,
				// otherwise serve the file from the plugin
				if ( $theme_file = locate_template( array ( 'single-dmci_cons_update.php' ) ) ) {
					$template_path = $theme_file;
				} else {
					$template_path = plugin_dir_path( __FILE__ ) . 'template/single-dmci_cons_update.php';
				}
			}
			else if( is_tax() ) {
				
				if ( $theme_file = locate_template( array ( 'cons-category.php' ) ) ) {
					$template_path = $theme_file;
				} else {
					$template_path = plugin_dir_path( __FILE__ ) . 'template/cons-category.php';
				}
				
			}
			
			
		}
		
		return $template_path;
		
	}
	
	
	
	add_action( 'init', 'create_my_taxonomies', 0 );
	
	function create_my_taxonomies() {
		register_taxonomy(
			'dmci_project_categories',
			'dmci_projects',
			array(
				'labels' => array(
					'name' => 'Project Category',
					'add_new_item' => 'Add New Category',
					'new_item_name' => "New Project Category"
				),
				'rewrite' => array( 'slug' => 'project-categories' ),
				'show_ui' => true,
				'show_tagcloud' => false,
				'hierarchical' => true
			)
    	);
	
		register_taxonomy(
				'dmci_project_locations',
				'dmci_projects',
				array(
					'labels' => array(
						'name' => 'Project Location',
						'add_new_item' => 'Add New Locartion',
						'new_item_name' => "New Project Location"
					),
					'show_ui' => true,
					'show_tagcloud' => false,
					'hierarchical' => true
				)
		);
		
	}
	
	add_action('init','add_query_var');
	function add_query_var() { 
		global $wp; 
		$wp->add_query_var('location-vicinity'); 
		$wp->add_query_var('building-features');
		$wp->add_query_var('unit-features');
		$wp->add_query_var('amenities');
		$wp->add_query_var('p_id');
	}
	
	function add_dmci_projects_columns($columns) {
		
   	 return array_merge($columns,
              array(
			  	'p_category' => __('Project Category'),
			  	'p_location' => __('Project Location'),
				));
	}
	add_filter('manage_dmci_projects_posts_columns' , 'add_dmci_projects_columns');
	
	function add_dmci_cons_update_columns($columns) {
		
   	 return array_merge($columns,
              array(
			  	'cons_update_date' => __('Construction Update Info')
				));
	}
	add_filter('manage_dmci_cons_update_posts_columns' , 'add_dmci_cons_update_columns');
	
	add_action( 'manage_posts_custom_column' , 'custom_columns' );
	function custom_columns( $column ) {
		global $post;
		//var_dump($post);
		switch ( $column ) {
			case 'cons_update_date':
				$metaData = get_post_meta( $post->ID, 'cons_update_date', true );
				echo 'Date: ' . $metaData . '<br/>' ;
				$query_args = array(
					'connected_type' => 'projects_to_consupdate',
					'connected_items' => intval(get_the_ID()),        
					'nopaging' => true
				);
				$project_query = new WP_Query($query_args);
				if( $project_query->have_posts() ){
					$project_query->the_post();	
					echo 'Project: ' . get_the_title();
				}
			break;
			case 'p_location' :
				$loc = wp_get_post_terms( get_the_ID(), 'dmci_project_locations', array('fields' => 'names')  );
				if( !empty($loc) )
					echo $loc[0];
			break;
			case 'p_category' :
				$cat = wp_get_post_terms( get_the_ID(), 'dmci_project_categories', array('fields' => 'names')  );

				if( !empty($cat) )
					echo $cat[0];
			break;
		}
	}
	
	function my_connection_types() {
		p2p_register_connection_type( array(
			'name' => 'projects_to_consupdate',
			'from' => 'dmci_cons_update',
			'to' => 'dmci_projects',
			'reciprocal' => true,
			'cardinality' => 'many-to-one',
			'prevent_duplicates' => false,
			'admin_box' => array(
				'show' => 'from',
				'can_create_post' => false,
			),
			
		) );
	}
	add_action( 'p2p_init', 'my_connection_types' );
	
	
	
	
   
?>